package com.example.java8.feature_05.func_interface;

import java.util.function.BiFunction;
import java.util.function.Function;

public class TestFunction {

    public static void main(String[] args){
        testFunction();
        testBiFunction();
    }

    /**
     * Function<T, R> 传⼊⼀个值经过函数的计算返回另⼀个值
     * T:入参类型
     * R:出参类型
     */
    public static void testFunction(){
        // 使用Lambda表达式传入方法体来获取Function实例对象
        Function<Integer, Integer> function = p -> p * 10;
        Integer result = function.apply(100);
        System.out.println(result);
    }

    /**
     * BiFunction<T, U, R>
     * <T> –函数的第一个参数的类型
     * <U> –函数的第二个参数的类型
     * <R> –函数结果的类型
     */
    public static void testBiFunction(){
        System.out.println(operator(10,21, (a,b) -> a + b));
        System.out.println(operator(10,2, (a,b) -> a - b));
        System.out.println(operator(8,4, (a,b) -> a * b));
        System.out.println(operator(10,2, (a,b) -> a / b));
    }

    public static Integer operator(Integer a, Integer b, BiFunction<Integer, Integer, Integer> bf) {
        return bf.apply(a, b);
    }
}
